package com.chinawayltd.ews.sample.util.map;

import com.chinawayltd.ews.sample.util.ReflectionsUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: greeProject.timingTaskCenter
 * @description:
 * @author: xiazj
 * @create: 2018-04-24 14:53
 **/
public class MapUtils extends org.apache.commons.collections.MapUtils {

    public static <T> Map<String, T> listToMap(List<T> list, String keyField){
        if(list == null){
            throw new RuntimeException("list must be not null!");
        }else if(StringUtils.isBlank(keyField)){
            throw new RuntimeException("keyField must be not blank!");
        }

        Map<String, T> result = new HashMap<>();
        for (T data : list) {
            Object keyValueObj = ReflectionsUtils.invokeGetter(data, keyField);
            if(keyValueObj != null){
                String key = keyValueObj.toString();
                result.put(key, data);
            }
        }
        return result;
    }

}
